<template>
  <el-dialog
    :close-on-click-modal="false"
    :title="isEdit ? '修改' : '新增'"
    width="800px"
    :visible.sync="visible"
  >
    <el-form :model="form" ref="form" :label-position="labelPosition" label-width="130px">
      <el-row :gutter="24">
        <el-col>
          <el-form-item label="操办事项">
            <el-input type="text" v-model="form.arrangeMatters" placeholder="请输入操办事项" />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="办理时间">
            <el-date-picker
              type="date"
              :style="inputWidth"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              v-model="form.arrangeDate"
              placeholder="请选择"
            />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="办理地点">
            <el-input type="text" v-model="form.arrangeAdr" placeholder="请输入办理地点" />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="礼金金额">
            <el-input :min="0" type="number" v-model="form.cashGift" />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="名称">
            <el-input type="text" v-model="form.name" />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="件数">
            <el-input :min="0" type="number" v-model="form.num" />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="折合人民币(元)">
            <el-input type="number" v-model="form.totalRmb" />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="处理情况">
            <el-input type="textarea" :rows="2" v-model="form.giftDeal" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="handleUpdate">{{ isEdit ? '修改' : '新增' }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { updateUserHappyFuneral, createUserHappyFuneral } from '@/api/archives'
import { openNotification, getSession } from '@/utils/util'

export default {
  data() {
    return {
      isEdit: false,
      visible: false,
      labelPosition: 'right',
      form: {
        userHappyFuneralGiftVO: []
      },
      inputWidth: {
        width: '100%'
      },
      leaderRow: JSON.parse(this.$route.query.leaderRow)
    }
  },
  methods: {
    edit(bool, obj) {
      this.isEdit = bool
      this.visible = true
      this.form = Object.assign({}, obj)
      this.$set(this.form, 'userId', this.leaderRow.userId)
    },
    handleUpdate() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          this.form.userHappyFuneralGiftVO = []
          let res = null,
            mes = null
          let { name, num, totalRmb } = this.form
          this.form.userHappyFuneralGiftVO.push({ name, num, totalRmb })
          if (this.isEdit) {
            res = await updateUserHappyFuneral(this.form)
            mes = '修改操作成功'
          } else {
            res = await createUserHappyFuneral(this.form)
            mes = '新增操作成功'
          }
          if (res.state === '000000') {
            openNotification('success', mes)
            this.visible = false
            this.$emit('refresh')
          }
        }
      })
    }
  }
}
</script>
